from werkzeug import Request, SharedDataMiddleware, ClosingIterator
from werkzeug.exceptions import HTTPException, NotFound
from utils import STATIC_PATH, local, local_manager, url_map

import views


class SilkWeb(object):
  def __init__(self):
    local.application = self

    self.dispatch = SharedDataMiddleware(self.dispatch, {
      '/static':  STATIC_PATH
    })

  def dispatch(self, environ, start_response):
    local.application = self
    request = Request(environ)
    local.url_adapter = adapter = url_map.bind_to_environ(environ)
    try:
      endpoint, values = adapter.match()
      handler = getattr(views, endpoint)
      response = handler(request, **values)
    except NotFound, e:
      response = views.not_found(request)
      response.status_code = 404
    except HTTPException, e:
      response = e
    return ClosingIterator(response(environ, start_response),
                           [local_manager.cleanup])

  def __call__(self, environ, start_response):
    return self.dispatch(environ, start_response)
